<?php
require_once(dirname(__FILE__)."/../core/safeParam.php");

$function = $_GET['function'];
$cid = $_GET['cid'];


switch($function){
	
	default:   
	
if(!isset($cid)){
  $results = $database->select("SELECT * FROM coupons WHERE rid = ".ARID." order by name "); 
   
    echo '	
    <p><a href="index.php?edit=coupons&function=add"><img src="add.png" border="0"> Add Coupon</a>   </p> 
    <table cellspacing="2" cellpadding="2" border="0" class="table"><tr>
                    <th>ID  </th>
                    <th>Name</th>
                    <th>Code</th>
            <th>Action</th>
            </tr>';		

  for($i=0; $i<count($results); $i++){

    echo '<tr>
        <td>'.$results[$i]['id'].'  </td>
        <td><a href="index.php?edit=coupons&function=edit&cid='.$results[$i]['id'].'">'.$results[$i]['name'].'  </a></td>
        <td>'.$results[$i]['code'].' </td>
                <td>
        <a href="index.php?edit=coupons&function=edit&cid='.$results[$i]['id'].'"><img src="application_edit.png"  border="0"> Edit </a> 
        <a href="../functions/coupons.php?function=delete&id='.$results[$i]['id'].'"><img src="delete.png"  border="0"> Delete </a> 
     </td>
    </tr>';		
                                                          
  }
  echo '</table>';

} else {
	$results = $database->select("SELECT * FROM coupons where id = $cid ");
	?>
	<form action="../functions/coupons.php?function=edit" method="post">
          <input type="hidden" name="id" value="<?php echo $results[0]['id']; ?>">
              <table width="98%" border="0" cellspacing="3" cellpadding="3"  class="table_form" style="text-align:left">
        <tr>
          <td>Coupon Name: </td>
          <td><input type="text" name="name" value="<?php echo $results[0]['name']; ?>"></td>
        </tr>
        <tr>
          <td>Coupon Code: (no spaces) </td>
          <td><input type="text" name="code" value="<?php echo $results[0]['code']; ?>"></td>
        </tr>
        <tr>
          <td>Coupon Type: </td>
          <td><select name="type"><option value="percent" <?php if($results[0]['type'] == 'percent'){ echo '  selected'; } ?>>percentage off total</option><option value="subtract"  <?php if($results[0]['type'] == 'subtract'){ echo '  selected'; } ?>>Subtract from total</option></select></td>
        </tr>
        <tr>
          <td>Coupon Discount: (only numbers) </td>
          <td><input type="text" name="discount" value="<?php echo $results[0]['discount']; ?>"></td>
        </tr>
           <tr>
          <td>Minimum total amount: </td>
          <td><input type="text" name="minimum" value="<?php echo $results[0]['minimum']; ?>"></td>
        </tr>
         <tr>
          <td>Coupon Description: </td>
          <td><textarea cols="45" rows="15" name="description" ><?php echo $results[0]['description']; ?></textarea></td>
        </tr>
          <tr>
          <td></td>
          <td><input type="submit" value="Save"></td>
        </tr>
      </table>
      </form>
          
<?php
          
  }

  break;
case "add" :
?>

  <form action="../functions/coupons.php?function=add" method="post">  <input type="hidden" name="rid" value="<?php echo ARID; ?>" />
    <table width="98%" border="0" cellspacing="3" cellpadding="3 "  class="table_form" style="text-align:left">
      <tr>
        <td>Coupon Name: </td>
        <td><input type="text" name="name" value="<?php echo $results[0]['name']; ?>"></td>
      </tr>
      <tr>
        <td>Coupon Code:(no spaces) </td>
        <td><input type="text" name="code" value="<?php echo $results[0]['code']; ?>"></td>
      </tr>
      <tr>
        <td>Coupon Type: </td>
        <td><select name="type"><option value="percent">percentage off total</option><option value="subtract">Subtract from total</option></select></td>
      </tr>
      <tr>
        <td>Coupon Discount: </td>
        <td><input type="text" name="discount" value="<?php echo $results[0]['discount']; ?>"></td>
      </tr>
        <tr>
        <td>Minimum total amount: </td>
        <td><input type="text" name="minimum" value="<?php echo $results[0]['minimum']; ?>"></td>
      </tr>
       <tr>
        <td>Coupon Description: </td>
       <td><textarea cols="45" rows="15" name="description" ><?php echo $results[0]['description']; ?></textarea></td>
      </tr>
        <tr>
        <td></td>
        <td><input type="submit" value="Save"></td>
      </tr>
    </table>
  </form>
<?php
break;

}
?>
